package com.psy.counseling.utils;

import com.psy.counseling.entity.Article;
import com.psy.counseling.service.ArticleService;
import com.psy.counseling.service.QuestionService;
import lombok.Data;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.annotation.Resource;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;

/**
 * <p>
 *  导入工具类
 * </p>
 *
 * @author 陈灿
 * @since 2022-06-07
 */
@Data
public class ImportArticleUtil {

    @Resource
    private QuestionService questionService;
    private ArrayList<Article> importSuccess;
    private ArrayList<Integer> importError;

    public ImportArticleUtil(FileInputStream inputStream) throws IOException {
        importError = new ArrayList<>();
        importSuccess = new ArrayList<Article>();
        ImportData(inputStream);
    }

    public void ImportData(FileInputStream inputStream) throws IOException {
        XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
        XSSFSheet sheet = workbook.getSheetAt(0);
        int lastRowNum = sheet.getLastRowNum();
        for (int i = 1; i <= lastRowNum; i++) {
            XSSFRow row = sheet.getRow(i);
            DecimalFormat df = new DecimalFormat("#");
            XSSFCell idCell = row.getCell(0);
            XSSFCell tnameCell = row.getCell(1);
            XSSFCell titleCell = row.getCell(2);
            XSSFCell contentCell = row.getCell(3);
            XSSFCell timeCell = row.getCell(4);
            if (idCell==null || tnameCell == null || titleCell==null || contentCell == null) {
                importError.add(i+1);
                continue;
            }
            Article article = new Article();
            String id = df.format(Double.parseDouble(idCell.toString()));
            article.setId(Long.valueOf(id));


            article.setTname(String.valueOf(tnameCell));


            article.setTitle(String.valueOf(titleCell));


            article.setContent(String.valueOf(contentCell));

            article.setTime(String.valueOf(timeCell));


            importSuccess.add(article);
        }
    }

}